package cms.student.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import cms.student.entity.Area;
import cms.student.entity.Student;
import cms.student.entity.Subject;
import cms.student.service.StudentService;

@Controller
public class StuController {

	@Autowired
	StudentService stuService;

	@RequestMapping("list")
	public String list(HttpServletRequest request, @RequestParam(defaultValue = "1") int page,
			@RequestParam(defaultValue = "") String name) {

		PageInfo<Student> pageStudent = stuService.list(page, name);
		request.setAttribute("pageStudent", pageStudent);

		return "list";
	}

	@RequestMapping("toAdd")
	public String toAdd(HttpServletRequest request) {
		List<Area> provinces = stuService.listAreasByParentId(1);
		List<Subject> subjects = stuService.listAllSubjects();

		request.setAttribute("provinces", provinces);
		request.setAttribute("subjects", subjects);

		return "add";
	}

	@RequestMapping("add")
	public String add(Student stu, int[] selSubId) {

		stuService.addStudent(stu, selSubId);

		return "redirect:list";
	}

	@RequestMapping("update")
	public String update(Student stu, int[] selSubId) {

		stuService.addStudent(stu, selSubId);

		return "redirect:list";
	}

	@RequestMapping("delBatch")
	@ResponseBody
	public String delBatch(@RequestParam(value = "ids") int[] ids) {
		stuService.delBatch(ids);
		return "success";
	}

	@RequestMapping("getAeas")
	@ResponseBody
	public List<Area> getAeas(int parentId) {
		List<Area> areas = stuService.listAreasByParentId(parentId);
		return areas;
	}

	@RequestMapping("toupdate")
	public String toupdate(HttpServletRequest request, int id) {
		Student stu = stuService.getById(id);

		request.setAttribute("stu", stu);

		List<Area> parentId = stuService.listAreasByParentId(1);

		List<Subject> subjects = stuService.listAllSubjects();

		request.setAttribute("parentId", parentId);

		request.setAttribute("subjects", subjects);

		return "update";
	}

}
